import { DeepPartial, FindManyOptions, UpdateResult } from "typeorm";
import { Board } from "../entities";
import { AppDataSource } from "../data-source";

export class BoardService {
  private static readonly boardRepository = AppDataSource.getRepository(Board);

  public static async create(payload: DeepPartial<Board>): Promise<Board> {
    const create = this.boardRepository.create(payload);
    return this.boardRepository.save(create);
  }

  public static async findById(id: Board["id"]): Promise<Board | null> {
    return this.boardRepository.findOne({ where: { id }, relations: ["user"] });
  }

  public static async find(
    options: FindManyOptions<Board>,
  ): Promise<[Board[], number]> {
    return this.boardRepository.findAndCount({
      ...options,
      relations: ["user"],
    });
  }

  public static async update(
    id: Board["id"],
    payload: DeepPartial<Board>,
  ): Promise<UpdateResult> {
    return this.boardRepository.update({ id }, payload);
  }

  public static async remove(board: Board): Promise<Board> {
    return this.boardRepository.remove(board);
  }
}
